# -*- coding: utf-8 -*-

#    OriginDB
#    Copyright (C) 2009  Jason Power <power.jg@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4 import QtWebKit
from pubchem import PubChemObj


class PubChemImport(QtGui.QDialog):

	def __init__(self, controller, compoundClass):
		QtGui.QDialog.__init__(self)

		self.controller = controller
		self.compoundClass = compoundClass

		self.setWindowTitle("PubChem Compound Import")

		self.backButton = QtGui.QPushButton("Back")

		self.web = QtWebKit.QWebView()
		self.web.load(QtCore.QUrl("http://pubchem.ncbi.nlm.nih.gov/search/search.cgi"))
		self.web.show()

		self.addCompoundButton = QtGui.QPushButton("Import Compound")
		self.addCompoundButton.setEnabled(False)

		self.loadProgress = QtGui.QProgressBar()
		self.loadProgress.setRange(0,100)

		self.cancelButton = QtGui.QPushButton("Cancel")

		self.vlayout = QtGui.QVBoxLayout()
		self.topLayout = QtGui.QHBoxLayout()
		self.topLayout.addWidget(self.backButton)
		self.topLayout.addStretch()
		self.vlayout.addLayout(self.topLayout)
		self.vlayout.addWidget(self.web)
		self.bottomLayout = QtGui.QHBoxLayout()
		self.bottomLayout.addWidget(self.loadProgress)
		self.bottomLayout.addStretch()
		self.bottomLayout.addWidget(self.addCompoundButton)
		self.bottomLayout.addWidget(self.cancelButton)
		self.vlayout.addLayout(self.bottomLayout)

		self.setLayout(self.vlayout)

		self.connect(self.backButton, QtCore.SIGNAL("clicked()"), self.web.back)
		self.connect(self.addCompoundButton, QtCore.SIGNAL("clicked()"), self.addCompound)
		self.connect(self.web, QtCore.SIGNAL("urlChanged(const QUrl &)"), self.checkPage)
		self.connect(self.web, QtCore.SIGNAL("loadProgress(int)"), self.loadProgress.setValue)
		self.connect(self.web, QtCore.SIGNAL("loadFinished(bool)"), self.loadFinished)
		self.connect(self.cancelButton, QtCore.SIGNAL("clicked()"), self.close)

	def sizeHint(self):
		return QtCore.QSize(900,650)

	def addCompound(self):
		url =  str(self.web.url())[19:]
		begin = url.find("cid=")
		end = url.find("&", begin)
		cid = int(url[begin+4:end])
		print "Cid is:", cid

		data = PubChemObj(cid)
		sdf = data.getSDF()

		end = sdf.find(">")
		molfile = sdf[0:end]

		begin = sdf.find("InChI")
		end = sdf.find("\n", begin)
		inchi = sdf[begin:end]

		begin = sdf.find("<PUBCHEM_MOLECULAR_FORMULA>")
		begin = sdf.find("\n", begin) + 1
		end = sdf.find("\n", begin)
		formula = sdf[begin:end]

		begin = sdf.find("<PUBCHEM_IUPAC_NAME>")
		begin = sdf.find("\n", begin) + 1
		end = sdf.find("\n", begin)
		iupac = sdf[begin:end]

		compound = self.compoundClass()
		compound.molfile = molfile
		compound.formula = formula
		compound.IUPACname = iupac
		compound.inchi = inchi

		self.controller.setViewCurrentCompound(compound)

		self.close()

	def checkPage(self, url):
		url = str(url)[19:]
		if url.startswith("http://pubchem.ncbi.nlm.nih.gov/summary/summary.cgi?") == False and \
				url.startswith("http://pubchem.ncbi.nlm.nih.gov/search/search.cgi") == False and \
				url.startswith("http://www.ncbi.nlm.nih.gov/sites/entrez?db=pccompound") == False:
			self.web.back()

		if url.startswith("http://pubchem.ncbi.nlm.nih.gov/summary/summary.cgi?"):
			self.addCompoundButton.setEnabled(True)
		else:
			self.addCompoundButton.setEnabled(False)

	def loadFinished(self, ok):
		self.loadProgress.reset()